package cn.gson.school.controller.jiaoxue;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.gson.school.dao.jiaoxue.ClassDao;
import cn.gson.school.dao.jiaoxue.StudentDao;
import cn.gson.school.dao.jiaoxue.ZhuanBanDao;
import cn.gson.school.entity.jiaoxue.ClassEntity;
import cn.gson.school.entity.jiaoxue.StudentEntity;
import cn.gson.school.entity.jiaoxue.ZhuanBanEntity;
import cn.gson.school.mappers.jiaoxue.ZhuanBanMappers;

@Controller
@RequestMapping("/zhuanban")
public class ZhuanBanController {

	@Autowired
	private ZhuanBanMappers ZhuanBanMappers;

	@Autowired
	private ZhuanBanDao zhuanBanDao;
	
	@Autowired
	private StudentDao studentDao;
	
	@Autowired
	private ClassDao classDao;

	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> index(ZhuanBanEntity zhuanBanEntity,
			@RequestParam(name = "page", defaultValue = "1") Integer pageNum,
			@RequestParam(name = "rows", defaultValue = "20") Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<ZhuanBanEntity> list = ZhuanBanMappers.zhuanbanList(zhuanBanEntity);
		PageInfo<ZhuanBanEntity> info = new PageInfo<>(list);
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("total", info.getTotal());
		obj.put("rows", info.getList());
		return obj;
	}
	
	@RequestMapping("save")
	@ResponseBody
	@Transactional
	public Map<String, Object> save(@Valid ZhuanBanEntity zhuanBanEntity,HttpServletRequest request,Model model,BindingResult br) {
		Map<String, Object> obj=new HashMap<>();
		obj.put("success", false);
		if (br.hasErrors()) {
			obj.put("message", "校验失败！");
			obj.put("errors", br.getFieldErrors());
		}else {
			String zhuanbanId=request.getParameter("zhuanBanId");
			String title=request.getParameter("title");
			String studentName=request.getParameter("studentName");
			String oldClass=request.getParameter("hid");
			String newClass=request.getParameter("newClass");
			StudentEntity student=studentDao.findByStudentName(studentName);
			ZhuanBanEntity zhuanBan;
			if (newClass==null||"".equals(newClass)) {
				obj.put("message", "必须要选择一个班级转换哦~");
				return obj;
			}
			System.out.println("学生中的添加的编号"+title);
			if (zhuanbanId==null||"".equals(zhuanbanId)||title!=null) {
				//添加
				zhuanBan=new ZhuanBanEntity();
				System.out.println("添加");
				student.setStuId(student.getStuId());
				student.setClassId(Long.valueOf(newClass));
				studentDao.save(student);
				List<StudentEntity> students=studentDao.findByClassId(Long.valueOf(newClass));
				if (students.size()==0) {
					ClassEntity classEntity=classDao.findOne(Long.valueOf(newClass));
					classEntity.setClassState("1");
					classDao.save(classEntity);
				}
			}else {
				//编辑
				System.out.println("编辑");
				zhuanBan=zhuanBanDao.findOne(Long.valueOf(zhuanbanId));
				zhuanBan.setZhuanbanId(Long.valueOf(zhuanbanId));
			}
			String type=request.getParameter("type");
			zhuanBan.setDelete(1);
			zhuanBan.setStudent(student);
			zhuanBan.setOldClassId(Long.valueOf(oldClass));
			zhuanBan.setNewClassId(Long.valueOf(newClass));
			zhuanBan.setType(Integer.valueOf(type));
			zhuanBan.setZhuangBanTime(new Date());
			student.setClassId(Long.valueOf(newClass));
			studentDao.save(student);
			zhuanBanDao.save(zhuanBan);
			obj.put("success", true);
		}
		return obj;
	}
	
	

	@GetMapping("delete/{id}")
	@ResponseBody
	@Transactional
	public Map<String, Object> delete(@PathVariable("id") Long id) {
		Map<String, Object> obj = new HashMap<String, Object>();
		obj.put("success", false);
		ZhuanBanEntity zhuanBanEntity = zhuanBanDao.findOne(id);
		if (id != null && (zhuanBanEntity) != null) {
			zhuanBanEntity.setDelete(0);
			zhuanBanDao.save(zhuanBanEntity);
			obj.put("success", true);
		} else {
			obj.put("message", "你要删除的记录不存在！");
		}
		return obj;
	}
}
